package com.learning.rocketmq.baseapi;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;

/**
 * ClassName: SyncProducer
 * Description:
 * Date: 2018/12/26 14:04 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class SyncProducer {
    public static void main(String[] args) throws MQClientException, UnsupportedEncodingException, RemotingException, InterruptedException, MQBrokerException {

        // 设置 producerGroup
        DefaultMQProducer producer = new DefaultMQProducer("base_producer_group");
        // 设置 nameServer
        producer.setNamesrvAddr("10.0.64.106:9876;10.0.64.107:9876");
        producer.start();

        for (int i = 0; i < 100; i++) {
            Message message = new Message();
            message.setTopic("base_topic");
            message.setTags("base_api");
            message.setBody(("Hello RocketMQ > " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));

            SendResult result = producer.send(message);
            System.out.printf("%s%n", result);
        }

        producer.shutdown();

    }
}
